package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.AddressService;
import services.CentralLaboratoryCRUDService;
import services.CriteriaCentralLaboratory;

import java.util.ArrayList;
import java.util.List;

@Namespace("/Directories/CentralLaboratory")
public class CentralLaboratoryController extends ActionSupport implements ModelDriven {

    private CentralLaboratoryCRUDService<LaboratorioCentral> centralLaboratoryService;
    private List<LaboratorioCentral>  list;
    private LaboratorioCentral laboratorioCentral = new LaboratorioCentral();
    private LaboratorioCentral editableLaboratory = new LaboratorioCentral();
    private String editableLabId;
    private List<Countries> countries = new ArrayList<Countries>();
    private List<Regions> regions = new ArrayList<Regions>();
    private List<Cities> cities = new ArrayList<Cities>();
    private AddressService addressService;
    private Countries countrySelected;
    private Regions regionSelected;
    private Cities citySelected;

    @Autowired
    public CentralLaboratoryController(CentralLaboratoryCRUDService<LaboratorioCentral> centralLaboratoryCRUDService, AddressService addressService) {
        this.centralLaboratoryService =  centralLaboratoryCRUDService;
        this.addressService = addressService;
        laboratorioCentral.setAddress(new Address());
        countries = addressService.getCountries();
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.central_laboratory.tiles", type="tiles")
    })
    public String execute() {
        list = new ArrayList<LaboratorioCentral>(0);
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.central_laboratory.tiles", type="tiles")
    })
    public String list() {

        CriteriaCentralLaboratory cr = new CriteriaCentralLaboratory(laboratorioCentral);
        list = centralLaboratoryService.list(cr);

        return SUCCESS;
    }

    @Action(value="edit", results={
            @Result(name="success",location="/directories.central_laboratory.edit.tiles", type="tiles")
    })
    public String editLaboratory() {
        editableLaboratory = centralLaboratoryService.findById(Integer.parseInt(editableLabId));
        countrySelected = addressService.getCountryByName(editableLaboratory.getAddress().getCountry());

        regions = addressService.getRegions(countrySelected.getCountryId());
        regionSelected = addressService.getRegionByName(editableLaboratory.getAddress().getProvince(), countrySelected.getCountryId().toString());

        cities = addressService.getCities(regionSelected.getRegionId());

        citySelected = addressService.getCityByName(editableLaboratory.getAddress().getCity(), regionSelected.getRegionId().toString(), countrySelected.getCountryId().toString());
        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.central_laboratory.create.tiles", type="tiles")
    })
    public String createLaboratory() {
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String saveLaboratory() {
        addressService.checkCity(   laboratorioCentral.getAddress().getCity(),
                                    laboratorioCentral.getAddress().getProvince(),
                                    laboratorioCentral.getAddress().getCountry());

        centralLaboratoryService.save(laboratorioCentral);
        return SUCCESS;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String deleteLaboratory() {
        centralLaboratoryService.delete(laboratorioCentral);
        return SUCCESS;
    }

    public LaboratorioCentral getEditableLaboratory() {
        return editableLaboratory;
    }

    public void setEditableLaboratory(LaboratorioCentral editableLaboratory) {
        this.editableLaboratory = editableLaboratory;
    }

    public String getEditableLabId() {
        return editableLabId;
    }

    public void setEditableLabId(String editableLabId) {
        this.editableLabId = editableLabId;
    }

    @Override
    public Object getModel() {
        return laboratorioCentral;
    }

    public List<LaboratorioCentral> getList() {
        return list;
    }

    public void setList(List<LaboratorioCentral> list) {
        this.list = list;
    }

    public List<Countries> getCountries() {
        return countries;
    }

    public void setCountries(List<Countries> countries) {
        this.countries = countries;
    }

    public Cities getCitySelected() {
        return citySelected;
    }

    public void setCitySelected(Cities citySelected) {
        this.citySelected = citySelected;
    }

    public Regions getRegionSelected() {
        return regionSelected;
    }

    public void setRegionSelected(Regions regionSelected) {
        this.regionSelected = regionSelected;
    }

    public Countries getCountrySelected() {
        return countrySelected;
    }

    public void setCountrySelected(Countries countrySelected) {
        this.countrySelected = countrySelected;
    }

    public List<Cities> getCities() {
        return cities;
    }

    public void setCities(List<Cities> cities) {
        this.cities = cities;
    }

    public List<Regions> getRegions() {
        return regions;
    }

    public void setRegions(List<Regions> regions) {
        this.regions = regions;
    }
}
